package com.fcscs.cservice.client.main.configuration.inroomdining;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.fcscs.core.common.authority.UserAuthority;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.ui.FcsFields.EasyClearCombobox;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.CserviceControllerUtils;
import com.fcscs.cservice.client.common.model.configuration.inroomdining.ChargeType;
import com.fcscs.cservice.client.common.model.configuration.inroomdining.ChargeTypeModelData;
import com.fcscs.cservice.client.common.model.configuration.reservation.PropertyIdNameModelData;
import com.google.gwt.user.client.ui.Widget;

public class ChargeTypePanel extends FcsPanel {

	private VerticalPanel vp;
	private EasyClearCombobox<PropertyIdNameModelData> propCombox;
	private ListStore<PropertyIdNameModelData> propListStore;
	private ChargeTypeGrid grid;
	private ChargeTypeDialog chargeTypeDialog;
	private FormButton addBtn;

	public ChargeTypePanel(String id) {
		super(id);
	}

	@Override
	public void init() {
		if (null == vp) {
			vp = new VerticalPanel();
			vp.setId("PaymentMethodPanel_vp");
			vp.setSpacing(5);

			HorizontalPanel hp = new HorizontalPanel();
			hp.setId("PaymentMethodPanel_hp");
			hp.setSpacing(5);

			Label propLabel = new Label(CoreConstants.CORE_CONSTANTS.property()
					+ CoreConstants.REQURIED_FIELD_LABEL);
			propLabel.setId("PaymentMethodPanel_propLabel");
			propLabel.setWidth(150);

			propListStore = new ListStore<PropertyIdNameModelData>();
			propCombox = new EasyClearCombobox<PropertyIdNameModelData>();
			propCombox.setId("PaymentMethodPanel_propCombox");
			// propCombox.setFieldLabel(CoreConstants.CORE_CONSTANTS.property());
			propCombox.setWidth(200);
			propCombox.setDisplayField(CoreConstants.FULLTNAME);
			propCombox.setAllowBlank(false);
			propCombox.setForceSelection(true);
			propCombox.setTriggerAction(TriggerAction.ALL);
			propCombox.setStore(propListStore);
			propCombox.addSelectionChangedListener(new SelectionChangedListener<PropertyIdNameModelData>() {

				@Override
				public void selectionChanged(SelectionChangedEvent<PropertyIdNameModelData> se) {
					PropertyIdNameModelData pinmmd = propCombox.getValue();
					if (pinmmd != null) {
						newSearch();
					}
				}
			});

			hp.add(propLabel);
			hp.add(propCombox);

			grid = new ChargeTypeGrid(CServiceConstants.CSERVICE_CONSTANTS.chargeType(), null);

			vp.add(hp);
			vp.add(grid);

			addBtn = new FormButton(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.add(),
					CServiceConstants.CSERVICE_CONSTANTS.chargeType()));
			addBtn.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "5px");
			addBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
				@Override
				public void componentSelected(ButtonEvent ce) {
					chargeTypeDialog = ChargeTypeDialog.getInstance(true);
					chargeTypeDialog.show();
				}
			});
			
			vp.add(addBtn);
			add(vp);
			onShow();
		}

	}

	@Override
	public UserAuthority getRequiredAuthority() {
		return null;
	}

	@Override
	protected Widget getViewPanel() {
		return null;
	}

	@Override
	public void onShow() {
		CserviceControllerUtils.loadPropertys(propListStore, propCombox);
	}

	@Override
	public boolean onHide() {
		return true;
	}
	
	public void newSearch() {
		grid.removeAllModels();
		List<ChargeTypeModelData> chargeTypeList = new ArrayList<ChargeTypeModelData>();
		ChargeType model1 = new ChargeType();
		model1.setChargetypeStr("charge Type1");
		model1.setCode("code1");
		chargeTypeList.add(new ChargeTypeModelData(model1));

		ChargeType model2 = new ChargeType();
		model2.setChargetypeStr("charge Type2");
		model2.setCode("code2");
		chargeTypeList.add(new ChargeTypeModelData(model2));
		grid.addModels(chargeTypeList);
	}
}
